/*
 * @Description: 前台登录中间件
 * @Author: zheng yiliang
 * @Date: 2021-05-28 22:59:17
 * @LastEditTime: 2021-05-28 23:14:16
 * @LastEditors: zheng yiliang
 */

const { SuccessModel, ErrorModel } = require('../util')
const axios = require('axios')
const urlencode = require('urlencode');

module.exports = (req, res, next) => {
  console.log(req.cookies)
  const token = req.cookies.access_token
  const opid = req.cookies.openid

  var qqAppID = '101950831';
  if (token && opid) {
    axios.get('https://graph.qq.com/user/get_user_info?access_token=' + urlencode(token) + '&oauth_consumer_key=' + urlencode(qqAppID) + '&openid=' + urlencode(opid)).then(user => {
      return next()
    }).catch(err => {
      return res.json(new ErrorModel('网路异常'))
    })
  } else {
    return res.json(new ErrorModel('尚未登录'))
  }
}